﻿Imports System.Drawing.Printing

''' <summary>
''' Form base thực hiện việc in báo cáo
''' </summary>
''' <remarks>Nguyễn Huy Thường added on 13/11/2010</remarks>
Public Class frmBasePrint

    ''' <summary>
    ''' Thuộc tính lưu tiêu đề của form khi được hiển thị
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 08/06/2012</remarks>
    Private m_Title As String
    Public Property Title() As String
        Get
            Return m_Title
        End Get
        Set(ByVal value As String)
            m_Title = value
        End Set
    End Property

    ''' <summary>
    ''' Thuộc tính lưu loại báo cáo sẽ được hiển thị
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 16/11/2010</remarks>
    Private m_ReportType As clscommon.ReportType
    Public Property ReportType() As clscommon.ReportType
        Get
            Return m_ReportType
        End Get
        Set(ByVal value As clscommon.ReportType)
            m_ReportType = value
        End Set
    End Property

    ''' <summary>
    ''' Thuộc tính lưu nguồn dữ liệu để fill vào report
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 16/11/2010</remarks>
    Private m_DataSource As DataTable
    Public Property DataSource() As DataTable
        Get
            Return m_DataSource
        End Get
        Set(ByVal value As DataTable)
            m_DataSource = value
        End Set
    End Property

    ''' <summary>
    ''' Thuộc tính lưu đối tượng lớp BL
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 08/06/2012</remarks>
    Private m_BusinessObject As BL.clsBLBase
    Public Property BusinessObject() As BL.clsBLBase
        Get
            Return m_BusinessObject
        End Get
        Set(ByVal value As BL.clsBLBase)
            m_BusinessObject = value
        End Set
    End Property

    Dim defaultPrinter As New PrinterSettings

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Me.Close()
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Try
            If vldCheckInput.Validate.IsValid Then
                If clscommon.CheckQuantity(txtCopies.Text) Then
                    defaultPrinter.PrinterName = cboPrinter.Text
                    If ReportType = clscommon.ReportType.AvailableItem Then
                        Dim availableItem As New AvailableItemList
                        With BusinessObject
                            .FillReport(.dsFMS.AvailableItem.TableName)
                            DataSource = .dsFMS.AvailableItem
                        End With
                        With availableItem
                            .PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4
                            .PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait
                            .SetDataSource(DataSource)
                            .PrintToPrinter(Integer.Parse(txtCopies.Text), False, 0, 0)
                        End With
                    ElseIf ReportType = clscommon.ReportType.AllItem Then
                        Dim allItem As New AllItemList
                        With BusinessObject
                            .FillReport(.dsFMS.AllItem.TableName)
                            DataSource = .dsFMS.AllItem
                        End With
                        With allItem
                            .PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4
                            .PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait
                            .SetDataSource(DataSource)
                            .PrintToPrinter(Integer.Parse(txtCopies.Text), False, 0, 0)
                        End With
                    End If
                    Me.Close()
                End If
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    Private Sub frmBasePrint_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            cboPrinter.Items.Clear()
            For Each installedPrinters As String In PrinterSettings.InstalledPrinters
                cboPrinter.Items.Add(installedPrinters)
            Next
            cboPrinter.Text = defaultPrinter.PrinterName
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Khi click vào dấu "+" hoặc "-" trên txtCopies thì thêm bớt số lượng
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 23/08/2011</remarks>
    Private Sub txtCopies_EditorButtonClick(sender As System.Object, e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles txtCopies.EditorButtonClick
        Try
            If e.Button.Key = "Add" Then
                If Not IsNumeric(txtCopies.Text) Or String.IsNullOrEmpty(txtCopies.Text) Then
                    txtCopies.Text = "1"
                Else
                    txtCopies.Text = (Int16.Parse(txtCopies.Text) + 1).ToString
                End If
            ElseIf e.Button.Key = "Subtract" Then
                If Not IsNumeric(txtCopies.Text) Or String.IsNullOrEmpty(txtCopies.Text) Or Integer.Parse(txtCopies.Text) <= "1" Then
                    txtCopies.Text = "1"
                Else
                    txtCopies.Text = (Int16.Parse(txtCopies.Text) - 1).ToString
                End If
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub
End Class